package com.lizard.schedule.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lizard.schedule.R;

/**
 * 页面中的行选项
 *
 * Created by wangyiqian on 15/12/17.
 */
public class ItemView extends FrameLayout {

    private TextView leftTv;

    private TextView rightTv;

    private ImageView arrow;

    public ItemView(Context context) {
        this(context, null);
    }

    public ItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.item, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.item_view);
        String leftTxt = typedArray.getString(R.styleable.item_view_left_txt);
        String rightTxt = typedArray.getString(R.styleable.item_view_right_txt);
        Boolean showArrow = typedArray.getBoolean(R.styleable.item_view_show_arrow, false);

        leftTv = (TextView) findViewById(R.id.left_tv);
        rightTv = (TextView) findViewById(R.id.right_tv);
        arrow = (ImageView) findViewById(R.id.arrow);

        if(showArrow){
            arrow.setVisibility(View.VISIBLE);
        } else {
            arrow.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(leftTxt)){
            leftTv.setText(leftTxt);
        }

        if(!TextUtils.isEmpty(rightTxt)){
            rightTv.setText(rightTxt);
        }

        typedArray.recycle();
    }

    public void setLeftTxt(String content){
        leftTv.setText(content);
    }

    public void setRightTxt(String content){
        rightTv.setText(content);
    }

    public void showArrow(boolean show){
        if(show){
            arrow.setVisibility(View.VISIBLE);
        } else{
            arrow.setVisibility(View.GONE);
        }
    }

    /**
     * 为右边箭头自定义图片资源
     *
     * @param res
     */
    public void setArrowIcon(int res){
        arrow.setImageResource(res);
    }

}
